package com.cn.util;

import com.cn.exception.MyException;
import com.cn.user.UserDO;

/**
 * @author FrozenWatermelon
 * @date 2020/7/16
 */
public class AuthUserContext {

    public final static String MEMBER_USER_ERROR = "非会员用户禁止操作";


    public final static String ADMIN_USER_ERROR = "非管理员用户禁止操作";
    /**
     * The request holder.
     */
    private static final ThreadLocal<UserDO> USER_INFO_IN_TOKEN_HOLDER = new ThreadLocal<>();

    public static UserDO get() {
        return USER_INFO_IN_TOKEN_HOLDER.get();
    }


    public static UserDO forceGet() {
        return USER_INFO_IN_TOKEN_HOLDER.get();
    }

    public static void set(UserDO userInfoInTokenBo) {
        USER_INFO_IN_TOKEN_HOLDER.set(userInfoInTokenBo);
    }

    public static void clean() {
        if (USER_INFO_IN_TOKEN_HOLDER.get() != null) {
            USER_INFO_IN_TOKEN_HOLDER.remove();
        }
    }

    /**
     * 检测是否管理员
     *
     * @param user
     */
    public static void checkAdmin(UserDO user) {
        if (user.getUserType() != 1) {
            throw new MyException(ADMIN_USER_ERROR);
        }
    }

    /**
     * 检测是否会员
     *
     * @param user
     */
    public static void checkMember(UserDO user) {
        if (user.getUserType() != 2) {
            throw new MyException(MEMBER_USER_ERROR);
        }
    }

}
